#import "BWWindowController.h"
#import "BWGraphDocument.h"


@implementation BWWindowController

+ (BWWindowController *) sharedInstance
{
    return (nil);
} // sharedInstance


+ (void) setSharedInstance: (BWWindowController *) controller
{
} // setSharedInstance


- (void) setDocument: (NSDocument *) document
{
    [super setDocument: document];

} // setDocument


- (void) windowDidLoad
{
    [super windowDidLoad];

    [self setShouldCascadeWindows: NO];
    [self setWindowFrameAutosaveName: [[self class] autosaveName]];

    [self setDocument: [self document]];

} // windowDidLoad


+ (NSString *) autosaveName
{
    return (@"autosaveName");
} // autosaveName


- (void) documentActivateNotification: (NSNotification *) notification
{
    NSDocument *document = [notification object];
    [self setDocument: document];

} // documentActivateNotification


- (void) documentDeactivateNotification: (NSNotification *) notification
{
    [self setDocument: nil];

} // documentDeactivateNotification


- (void) show
{
    [self setDocument: [[NSDocumentController sharedDocumentController]
                           currentDocument]];
    [self showWindow: self];

} // showOrHIde


- (void) showOrHide
{
    if ([[self window] isVisible]) {
        [[self window] orderOut: self];
    } else {
        [self show];
    }

} // showOrHide


+ (BWWindowController *) sharedController
{
    BWWindowController *controller = [self sharedInstance];

    if (controller == nil) {
        controller = [[self alloc] 
                         initWithWindowNibName: [self windowNibName]];
        [self setSharedInstance: controller];

        NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
        
        [center addObserver: controller
                selector: @selector(documentActivateNotification:)
                name: BWGraphDocument_DocumentActivateNotification
                object: nil];
        
        [center addObserver: controller
                selector: @selector(documentDeactivateNotification:)
                name: BWGraphDocument_DocumentDeactivateNotification
                object: nil];
    }

    return (controller);

} // sharedController


- (void) dealloc
{
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    [center removeObserver: self
            name: nil
            object: nil];

    [super dealloc];

} // dealloc


@end // BWWindowController

